/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view.leftPanels;

import controller.Data;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import jsc.independentsamples.TwoSampleTtest;
import jsc.tests.H1;
import weka.core.Instances;

/**
 *
 * @author Vlada
 */
public class TwoSamplesChooserPnlLeft extends javax.swing.JPanel {

    private ArrayList<Integer> indices;
    private ArrayList<String> attributesNumeric;
    private Instances instances;
    private double[] elementsFirstAttribute;
    private double[] elementsSecondAttribute;

    private boolean hasNumeric;

    /**
     * Creates new form TwoSamplesChooserPnlLeft
     */
    public TwoSamplesChooserPnlLeft() {
        initComponents();

        instances = Data.getInstance().getInstances();
        attributesNumeric = new ArrayList<>();
        indices = new ArrayList<>();

        for (int i = 0; i < instances.numAttributes(); i++) {
            if (instances.attribute(i).isNumeric()) {
                attributesNumeric.add(instances.attribute(i).name());
                indices.add(i);
            }
        }
        hasNumeric = false;
        for (int i = 0; i < attributesNumeric.size(); i++) {
            cmbAttribute1.addItem(attributesNumeric.get(i));
            cmbAttribute2.addItem(attributesNumeric.get(i));
            hasNumeric = true;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmbAttribute1 = new javax.swing.JComboBox();
        lblAttribute2 = new javax.swing.JLabel();
        cmbAttribute2 = new javax.swing.JComboBox();
        btnStart = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        lblAttribute2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblAttribute2.setText("Choose second attribute:");

        btnStart.setText("Start");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel1.setText("Results:");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Choose first attribute:");
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(61, 61, 61)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cmbAttribute2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnStart, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(61, 61, 61)
                                .addComponent(cmbAttribute1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(33, 33, 33)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblAttribute2))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cmbAttribute1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAttribute2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cmbAttribute2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(btnStart)
                .addGap(70, 70, 70)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(46, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        if (!hasNumeric) {
            JOptionPane.showMessageDialog(null, "There are only nominal attributes in table.");
            return;
        }

        String attName1 = cmbAttribute1.getSelectedItem().toString();
        String attName2 = cmbAttribute2.getSelectedItem().toString();
        
        if (attName1.equals(attName2)) {
            JOptionPane.showMessageDialog(null, "You have to choose different attributes.");
            return;
        }

        int index1 = 0;
        int index2 = 0;
        for (int i = 0; i < attributesNumeric.size(); i++) {
            if (attributesNumeric.get(i).equals(attName1)) {
                index1 = indices.get(i);
            }
            if (attributesNumeric.get(i).equals(attName2)) {
                index2 = indices.get(i);
            }
        }

        int n = instances.numInstances();
        elementsFirstAttribute = new double[n];
        elementsSecondAttribute = new double[n];
        for (int i = 0; i < n; i++) {

            elementsFirstAttribute[i] = instances.instance(i).value(index1);
            elementsSecondAttribute[i] = instances.instance(i).value(index2);
        }

        //TODO ovde ide pozivanje metoda za statisticku obradu
        TwoSampleTtest t = new TwoSampleTtest(elementsFirstAttribute, elementsSecondAttribute ,H1.NOT_EQUAL, false, 0.95);
        
            double up = t.getUpperLimit();
            double dw = t.getLowerLimit();
            double st = t.getStatistic();
            
            if (st >= dw && st <= up)  jTextArea1.setText("We accept H0");
		else jTextArea1.setText("We reject H0");
    }//GEN-LAST:event_btnStartActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnStart;
    private javax.swing.JComboBox cmbAttribute1;
    private javax.swing.JComboBox cmbAttribute2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel lblAttribute2;
    // End of variables declaration//GEN-END:variables

}
